﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using PSE.Framework.Versions.Common.Entities;
using PSE.Framework.Versions.Common;
using PSE.Framework.Versions.Common.Gateway;

namespace PSE.Framework.Versions.Engine
{
    public class LocalDiscovery : IDiscoveryVersions
    {
        public string _productName;
        public int _majorVersion;
        public string _productVersion;
        public string _serverName;

        public Product Get(string productName, int majorVersion, string serverName)
        {
            if (string.IsNullOrEmpty(productName))
                throw new ArgumentNullException("productName");

            _productName = productName;
            _majorVersion = majorVersion;
            _productVersion = Util.Instance.CombineSystemNameAndMajorVersion(_productName, _majorVersion);
            _serverName = serverName;
            Product product = new Product();
            product.Name = productName;
            product.MajorVersion = majorVersion;
            product.Modules = GetModules(string.Format(Constants.REGISTRY_SYSTEM_MODULES_PATH, _productVersion));

            return product;
        }

        private List<Module> GetModules(string path)
        {
            List<Module> modules = new List<Module>();

            IList<string> folders = RegistryUtil.Instance.GetFolders(path);
            if (folders == null || folders.Count == 0)
                return modules;

            foreach (string folder in folders)
            {
                string modulePath = path + @"\" + folder;
                Module module = new Module();
                module.Folder = folder;
                module.ModuleName = GetModuleName(path + @"\" + folder, false);
                module.Versions = new Module.Version
                {
                    ModuleVersion = GetSystemVersion(path + @"\" + folder, false),
                    ModuleName = module.ModuleName,
                    ServerUrl = this._serverName,
                    Server = RegistryUtil.Instance.GetValue(modulePath + @"\" + "Server", "Version", false),
                    Client = RegistryUtil.Instance.GetValue(modulePath + @"\" + "Client", "Version", false),
                    Database = string.Empty
                };
                module.Product = new Product
                {
                    Name = this._productName,
                    MajorVersion = this._majorVersion
                };
                modules.Add(module);
            }
            return modules;
        }

        private string GetSystemVersion(string path)
        {
            return GetSystemVersion(path, true);
        }

        private string GetSystemVersion(string path, bool throwException)
        {
            return RegistryUtil.Instance.GetValue(path, Constants.REGISTRY_KEY_VERSION, throwException);
        }

        private string GetModuleName(string path, bool throwException)
        {
            return RegistryUtil.Instance.GetValue(path, Constants.REGISTRY_KEY_MODULE_NAME, throwException);
        }
    }
}